<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Controller extends Controller {

	protected $restricted = true;
	protected $restricted_to_admin = false;
	
	protected $loginPage = "login";
	
	protected $user_id = null;
	protected $userdata = null;
	protected $fromUser; 
	protected $isOwner; 
	protected $isAdmin; 

	protected $tblu;
	protected $tblm;
	protected $tblt;
	protected $tblp;
	protected $tble;
	protected $tblf;

    function MY_Controller() {
    	
        parent::Controller();

        // TODO debug-flag in config
        // $this->output->enable_profiler(TRUE);
        
        $this->lang->load('common');
        
        // TODO check if view is available
		$viewname = $this->session->userdata(USERDATA_SESSIONPREFIX.'viewname');
		if (isset($viewname) && strlen($viewname) > 0) {
			$this->config->set_item('viewname', $viewname);
		}

		$this->ci =& get_instance();
		
		// Timezone
		date_default_timezone_set($this->ci->config->item('timezone'));
		// Locale
		
		// define tables
        $this->tblc = $this->db->dbprefix('config');
		$this->tblu = $this->db->dbprefix('user');
        $this->tblm = $this->db->dbprefix('matches');
        $this->tblx = $this->db->dbprefix('teams');
        $this->tblt = $this->db->dbprefix('predictions');
        $this->tblp = $this->db->dbprefix('points');
        $this->tble = $this->db->dbprefix('extra');
		$this->tblf = $this->db->dbprefix('forum');

		// Is Controller Access restricted
		if ($this->restricted) {
			
			// Is SessionData available
			if($this->session->userdata(USERDATA_SESSIONPREFIX.'username') == null) {

				// Save uri to redirect after login 
				$redirect = $this->uri->uri_string;
				$this->session->set_flashdata("redirect", $redirect);
				
				redirect($this->loginPage);
				
			} else {
				
				// Set data in controller
				$this->userdata = $this->getUserdataObjectFromSession(); 
				$this->user_id = $this->userdata->id;

				$this->fromUser = $this->session->userdata('fromUser');
				$this->isOwner = ($this->user_id == $this->fromUser);
				$this->isAdmin = ($this->userdata->group === 'admin');
				
				
				if ($this->restricted_to_admin && !$this->isAdmin) {
					die("Only for Admin");			
				}
				
			}
		}
		
		
    }

    protected function getUserdataObjectFromSession() {
        $u->id = $this->session->userdata(USERDATA_SESSIONPREFIX.'id'); 
        $u->username = $this->session->userdata(USERDATA_SESSIONPREFIX.'username');
        $u->name = $this->session->userdata(USERDATA_SESSIONPREFIX.'name');
        $u->email = $this->session->userdata(USERDATA_SESSIONPREFIX.'email');
        $u->group = $this->session->userdata(USERDATA_SESSIONPREFIX.'groupname');
		return $u;
    }


    protected function getUser($redirect) {
        
		$sql = "SELECT id, name, email FROM ".$this->tblu." ORDER BY name";
		$query = $this->db->query($sql);
		$d['user'] = $query->result();
		$d['redirect'] = $redirect;
		$d['userdata'] = $this->userdata;
		$d['fromUser'] = $this->session->userdata('fromUser');
		$d['isOwner'] = ($d['fromUser'] == $this->user_id);  
  		return $d;
    }

    protected function getData($goto) {
		$data = array();
		$this->ci =& get_instance();

		$data['maintitle'] = $this->ci->config->item('main_title');
		$data['title'] = lang("nav_".$goto);
		$data['version'] = $this->ci->config->item('version');
		$data['meta_title'] = $data['maintitle']." - ".$data['title'];
		$data['restricted'] = $this->restricted;
		$data['isAdmin'] = $this->isAdmin;
		
		$data['navitems'] = $this->ci->config->item('nav');
		$data['goto'] = $goto;
		$data['agoto'] = ""; // admin goto

		$f['footer'] = $this->ci->config->item('footer');
		$data['footer'] = $this->load->view('footer', $f, true);

		// Show userdata
		$data['userdata'] = $this->userdata; 
		
		$status = $this->session->flashdata("status");

		if (strlen($status) > 0)
			$data['status'] = $status;

		return $data;
    }

}
